#include "shopping.h"
#include<iostream>
std::string getTimeStr(time_t time){
    tm *m=localtime(&time);//时间格式转换
    char s[1024]={0};
    sprintf(s,"%d年%d月%d日 %d:%d:%d",m->tm_year+1900,m->tm_mon+1,m->tm_mday,m->tm_hour,m->tm_min,m->tm_sec);
    return std::string(s);
}


cartSystem::~cartSystem()
{
    while(cart.isEmpty()==false)
    {
        goods *c=(goods*)*cart.getFront();
        delete c;
        cart.pop();
    }
     while(recycleBin.isEmpty()==false)
    {
        goods *c=(goods*)*recycleBin.getTop();
        delete c;
        recycleBin.pop();
    }

}

void cartSystem::addGoods()
{
    goods *c=new goods("");
    std::cout<<"请输入你要购买的商品名称：";
    std::cin>>c->name;
    std::cout<<"请输入你要购买的商品价格：";
    std::cin>>c->price;
    c->buytime=time(nullptr);
    std::cout<<"商品|"<<c->name<<"|价格:"<<c->price<<"在"<<getTimeStr(c->buytime)<<"被添加进购物车\n";
    cart.push(c);

}

void cartSystem::deleteGoods()
{
    if(cart.isEmpty()==true)
    {
        std::cout<<"没有可以删除商品"<<std::endl;
        return;
    }
    std::cout<<"请输入要删除的商品：";
    std::string deletegoods;
    std::cin>>deletegoods;
    linkQueen tmpcart;
    while(cart.isEmpty()==false)
    {
        goods *c=(goods*)*cart.getFront();
        cart.pop();
        if(c->name==deletegoods)
        {
           recycleBin.push(c);        
        }     
        else
            tmpcart.push(c);
        
    }
    while(tmpcart.isEmpty()==false)
    {
        cart.push(*tmpcart.getFront());
        tmpcart.pop();
    }


}

void cartSystem::backDelete()
{
    if(recycleBin.isEmpty()==true)
    {
        std::cout<<"没有可以撤销删除的商品"<<std::endl;
        return;
    }
    std::cout<<"请输入要撤销删除的商品：";
    std::string backgoods;
    std::cin>>backgoods;
    linkStack tmpbin;
    while(recycleBin.isEmpty()==false)
    {
        goods *c=(goods*)*recycleBin.getTop();
        recycleBin.pop();
        if(c->name==backgoods)
        {
            cart.push(c);
        }
        else
            tmpbin.push(c);

    }
    while(tmpbin.isEmpty()==false)
    {
        recycleBin.push(*tmpbin.getTop());
        tmpbin.pop();
    }

}

void cartSystem::show()
{
    linkStack tmpbin;
    while(recycleBin.isEmpty()==false)
    {
        goods *c=(goods*)*recycleBin.getTop();
        recycleBin.pop();
        tmpbin.push(c);
        std::cout<<"回收站商品|"<<c->name<<"价格："<<c->price<<"|"<<std::endl;
    }
    while(tmpbin.isEmpty()==false)
    {
        goods *c=(goods*)*tmpbin.getTop();
      
        recycleBin.push(c);
        tmpbin.pop();
    }
    linkQueen tmpcart;
    while(cart.isEmpty()==false)
    {
        tmpcart.push(*cart.getFront());
        cart.pop();
    }
    while(tmpcart.isEmpty()==false)
    {
         goods *c=(goods*)*tmpcart.getFront();
         std::cout<<"购物车商品|"<<c->name<<"价格:"<<c->price<<"|"<<std::endl;
         cart.push(c);
         tmpcart.pop();
    }
}
